package presentation.control;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import business.exceptions.UnauthorizedException;

import presentation.data.MessagesUtil;
import presentation.gui.CartItemsWindow;
import presentation.gui.MaintainCatalogTypes;
import presentation.gui.MaintainProductCatalog;
import presentation.gui.SelectOrderWindow;
import presentation.gui.ShippingBillingWindow;

public class Authorization {
	
	public static Map<Class<? extends Component>, Boolean> acl 
		= new HashMap<Class<? extends Component>, Boolean>();
    
    static {
    	//acl map tells you whether a user aiming at window (key) requires
    	//admin authorization to arrive there 
    	acl.put(ShippingBillingWindow.class, Boolean.FALSE);
    	acl.put(CartItemsWindow.class, Boolean.FALSE);
    	acl.put(SelectOrderWindow.class, Boolean.FALSE);
    	acl.put(MaintainCatalogTypes.class, Boolean.TRUE);
    	acl.put(MaintainProductCatalog.class, Boolean.TRUE);   	
    }
    
    private static boolean requiresAdmin(Component c) {
    	if(c == null) return false;
    	Class<? extends Component> input = c.getClass();
    	if(!acl.containsKey(input)) return false;
    	return acl.get(input);
    }
    
    public static void checkAuthorization(Component c, boolean custIsAdmin) 
    		throws UnauthorizedException {
    	boolean requiresAdmin = requiresAdmin(c);
    	if(requiresAdmin && !custIsAdmin) 
    		throw new UnauthorizedException(MessagesUtil.NOT_AUTHORIZED);
    }   
}
